#include <iostream>
#include <fstream>
#include <sstream>
#include <Lexer.h>
#include <Parser.h>
#include <resource.h>
#include <cassert>
#include <llvmir.h>
int main(int argc, char **argv)
{
    std::string fileContent = R"(
    const a[10] = {1,2,3};
    var b = 1;
    procedure test;
    var test1 = 3;
    begin
        read(test1);
        if test1 > 2 then
            write(test1);
        else
            write(b);
        end;
    end;

    begin
        var t1 = 1;
        read(t1);
        write(t1);
        call test;
    end
)";

    // std::cout<<fileContent<<std::endl;
    // while(1);
    // // 输入的是一个文件名，检查后缀是否为 .pl0
    // if (argc != 2)
    // {
    //     std::cout << "Usage: " << argv[0] << " <filename>" << std::endl;
    //     return 1;
    // }

    // std::string filename = argv[1];
    // if (filename.size() < 4 || filename.substr(filename.size() - 4) != ".pl0")
    // {
    //     std::cout << "Error: file must have a .pl0 extension." << std::endl;
    //     return 1;
    // }

    // // 读取文件内容存储到字符串中
    // std::ifstream file(filename);
    // if (!file)
    // {
    //     std::cout << "Error: could not open file " << filename << std::endl;
    //     return 1;
    // }

    // std::stringstream buffer;
    // buffer << file.rdbuf();
    // std::string fileContent = buffer.str();

    // std::cout << fileContent << std::endl;

    LEXER::Lexer lexer(fileContent);
    std::vector<LEXER::Token> tokens = lexer.lex(fileContent);

    for (auto token : tokens)
    {
        std::cout << token << std::endl;
    }
    PARSER::Parser parser = PARSER::Parser(tokens);
    std::shared_ptr<PARSER::Program> program = parser.parse_program();
    program->boot_block = parser.adjust_boot_block(program->boot_block);
    program->boot_block->print_AstNode();
    LLVMIR::add_printf_and_scanf();
    parser.generate_llvmir(program->boot_block, 0);
    std::cout << std::endl << "over" << std::endl;
    LLVMIR::print_llvm_code();
    LLVMIR::write_llvm_code_to_file("output.ll");
    return 0;
}
